package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Counted_reservation_dao;
import com.clinic.domain.Counted_reservation;
import com.clinic.util.Util_imp;

public class Counted_reservation_imp implements Counted_reservation_dao {

	SessionFactory session_factory = null;

	public Counted_reservation_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_counted_reservation(Counted_reservation reservation) {
		System.out.println("Enter - create_counted_reservation");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int cd_obj_id = 0;
		try {
			tx = session.beginTransaction();
			cd_obj_id = (Integer) session.save(reservation);
			tx.commit();
			System.out.println("New Counted_reservation Created with ID: " + cd_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Counted_reservation Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_counted_reservation");
		return cd_obj_id;
	}

	@Override
	public void update_counted_reservation(Counted_reservation cn) {
		System.out.println("Enter - update_counted_reservation");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(cn);
			tx.commit();
			System.out.println("Counted_reservation Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Counted_reservation Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_counted_reservation");
	}

	@Override
	public void delete_counted_reservation(int cn_obj_id) {
		System.out.println("Enter - delete_counted_reservation");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Counted_reservation cn = (Counted_reservation) session.get(Counted_reservation.class, cn_obj_id);
			session.delete(cn);
			tx.commit();
			System.out.println("Counted_reservation with ID: " + cn_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Counted_reservation Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_counted_reservation");
	}

	@Override
	public Counted_reservation get_counted_reservation(int cn_obj_id) {
		System.out.println("Enter - get_counted_reservation");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Counted_reservation cn = null;
		try {
			tx = session.beginTransaction();
			cn = (Counted_reservation) session.createQuery("from Counted_reservation where cn_obj_id=:id")
					.setParameter("id", cn_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Date_of_reservation: " + cn.getDate_of_reservation());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_counted_reservation");
		return cn;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Counted_reservation> get_all_counted_reservation() {
		System.out.println("Enter - get_all_counted_reservation");
		Session session = session_factory.openSession();
		List<Counted_reservation> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Counted_reservation").list();
			tx.commit();
			System.out.println("Number of Counted_reservation returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_counted_reservation");
		return l;
	}

	@Override
	public Counted_reservation persistOrMerge(Counted_reservation c) {
		return (Counted_reservation) session_factory.getCurrentSession().merge(c);
	}

	@Override
	public int getsize() {
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Counted_reservation").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Counted_reservation: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}